<template>
  <div class="app-container">

    <el-form ref="param" :inline="true" :model="param" class="demo-form-inline">
      <el-form-item prop="name">
        <el-input v-model="param.name" placeholder="姓名" />
      </el-form-item>

      <el-form-item prop="createDate">
        <el-date-picker
          v-model="param.createDate"
          unlink-panels
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="onSearch">查询</el-button>
        <el-button icon="el-icon-delete" @click="onClear('param')">清空</el-button>
        <el-button type="success" icon="el-icon-document-add" @click="add">添加</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="加载中..."
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column
        label="序号"
        type="index"
        width="50"
      />
      <el-table-column
        property="name"
        label="姓名"
        width="120"
      />
      <el-table-column
        property="roleName"
        label="角色"
      />
      <el-table-column
        label="创建时间"
        width="180">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span style="margin-left: 10px">{{ scope.row.createDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-view"
            @click="show(scope.$index, scope.row)"
          >
            查看
          </el-button>
          <el-button
            type="warning"
            size="mini"
            icon="el-icon-edit"
            @click="update(scope.$index, scope.row)"
          >
            修改
          </el-button>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete-solid"
            @click="remove(scope.$index, scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <br>

    <el-pagination
      background
      layout="total, sizes, prev, pager, next"
      :page-sizes="[10, 20, 30, 50, 100]"
      :current-page="param.pageNum"
      :total="page.total"
      @current-change="pageNumChange"
      @size-change="pageSizeChange"
    />

    <el-dialog
      title="详细信息"
      :visible.sync="dialogVisible"
    >
      <el-row>
        <el-col :span="4">
          <el-image
            style="width: 100px; height: 100px"
            :src="userInfo.headImg"
            :preview-src-list="srcList"
          />
        </el-col>
        <el-col :span="12">
          <p>姓名 : {{ userInfo.name }}</p>
          <p>角色 : {{ userInfo.roleName }}</p>
        </el-col>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>

    <user-dialog ref="userDialog" :user-form="userForm" @addUser="addUser" />
  </div>
</template>

<script>
import { findList, addOrUpdate, findSysUserById, deleteSysUserById } from '@/api/user'
import UserDialog from './component/UserDialog.vue'
export default {
  components: {
    UserDialog
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      param: {
        pageNum: 1,
        pageSize: 10,
        name: '',
        createDate: []
      },
      page: {
        total: 0
      },
      list: [],
      listLoading: true,
      dialogVisible: false,
      userInfo: {
        name: '',
        roleName: '',
        headImg: ''
      },
      srcList: [],
      userForm: {
        id: '',
        name: '',
        roleId: ''
      }
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      this.listLoading = true
      findList(this.param).then(res => {
        this.list = res.data.list
        this.page.total = res.data.total
        this.listLoading = false
      })
    },
    show(index, row) {
      this.srcList = []
      this.srcList.push(row.headImg)
      this.userInfo.name = row.name
      this.userInfo.roleName = row.roleName
      this.userInfo.headImg = row.headImg
      this.dialogVisible = true
    },
    onSearch() {
      this.param.pageNum = 1
      this.init()
    },
    onClear(formName) {
      this.$refs[formName].resetFields()
    },
    pageNumChange(index) {
      this.param.pageNum = index
      this.init()
    },
    pageSizeChange(index) {
      this.param.pageSize = index
      this.init()
    },
    add() {
      this.$refs.userDialog.dialogVisible = true
    },
    addUser(userFormData) {
      addOrUpdate(userFormData).then(res => {
        this.$message({
          message: res.msg,
          type: 'success'
        })
        this.$refs.userDialog.dialogVisible = false
        // 刷新页面
        this.param.pageNum = 1
        this.init()
      })
    },
    update(index, row) {
      findSysUserById({ id: row.id }).then(res => {
        this.$refs.userDialog.dialogVisible = true
        this.$nextTick(() => {
          this.userForm = res.data
        })
      })
    },
    remove(index, row) {
      this.$confirm('此操作将删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteSysUserById({ id: row.id }).then(res => {
          this.$message({
            message: res.msg,
            type: 'success'
          })
          this.init()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
